using Glpt.Common;
using Glpt.CommSearch;
using Glpt.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

namespace Glpt.Task
{
    /// <summary>
    /// 文件入库任务
    /// </summary>
    public class ImportFileTask
    {
        private BackgroundWorker bwt = null;
        private Dictionary<string, string> dateCache = new Dictionary<string, string>();//缓存
        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        public ImportFileTask()
        {
        }
        /// <summary>
        /// 测试接口
        /// </summary>
        /// <param name="function_id">功能号</param>
        /// <param name="tradedate">业务日期</param>
        public void SetParams(string function_id, string tradedate)
        {
            SetParams(0, function_id, "", tradedate);
        }
        /// <summary>
        /// 参数设置
        /// </summary>
        /// <param name="db_no">数据库编号</param>
        /// <param name="function_id">功能编号</param>
        /// <param name="task_id">任务编号</param>
        /// <param name="tradedate">业务日期</param>
        public void SetParams(int db_no, string function_id, string task_id, string tradedate)
        {
            BaseInterFace bif = new BaseInterFace(db_no);
            string sql = "SELECT * FROM TB_IMPORT_LIST WHERE TASK_FUNCTION_ID=" + function_id + " AND ENABLE_FLAG='1' ORDER BY IMPORT_ID";
            DataTable dt_list = bif.ExecSelect(sql);
            DataRow[] drs = dt_list.Select("IMPORT_ID=-1");//动态取
            if (drs.Length > 0)
                dt_list = CommFuns.GetDynamicProc(dt_list, drs[0]["SOURCE_FILE"].ToString(), function_id, tradedate);
            foreach (DataRow dr in dt_list.Rows)
            {
                if (bwt != null && bwt.CancellationPending)
                    throw new Exception("线程被取消");
                try
                {
                    string BUSI_DATE = CommFuns.GetDateExpResult(bif, dr["DATE_EXP"].ToString().Trim(), tradedate, dateCache);
                    string source = Funcs.ReplaceWord(dr["SOURCE_FILE"].ToString(), BUSI_DATE).Trim();
                    string func_id = Funcs.ReplaceWord(dr["FUNCTION_ID"].ToString(), BUSI_DATE).Trim();
                    CommSearchInfo csinfo = new CommSearchInfo(func_id, false);//获取导入功能
                    string[] inputName = dr["PARAMS_NAME"].ToString().Split(new char[] { ';' });
                    string[] inputValue = dr["PARAMS_VALUE"].ToString().Split(new char[] { ';' });

                    for (int i = 0; i < csinfo.Input.Count; i++)
                    {
                        if (csinfo.Input[i].INPUT_TYPE == "3")
                            csinfo.Input[i].DEFAULT_VALUE = BUSI_DATE;
                        if (csinfo.Input[i].VAR_NAME.Equals("FILE_NAME"))
                        {
                            csinfo.Input[i].InputValue = new System.IO.FileInfo(source).Name;
                        }

                        for (int k = 0; k < inputName.Length; k++)
                        {
                            if (csinfo.Input[i].VAR_NAME.Equals(inputName[k]))
                            {
                                csinfo.Input[i].CurServerTime = Funcs.StrToDataTime(BUSI_DATE);
                                csinfo.Input[i].DEFAULT_VALUE = inputValue[k];
                                break;
                            }
                        }
                    }
                    csinfo.InputValue = csinfo.GetAllInputValue(false);
                    csinfo.ResultTable = csinfo.ReadFileData(source);
                    //数据导入
                    csinfo.SaveResult(csinfo.InputValue);
                }
                catch (Exception ex)
                {
                    throw new Exception("IMPORT_ID=" + dr["IMPORT_ID"].ToString() + "，" + ex.Message, ex);
                }
            }
        }

        /// <summary>
        /// 后台任务对象
        /// </summary>
        public BackgroundWorker BackgroundWorkerTask
        {
            get
            {
                return bwt;
            }
            set
            {
                bwt = value;
            }
        }

        /// <summary>
        /// 释放内存
        /// </summary>
        public void Dispose()
        {
            try
            {
                GC.Collect();
            }
            catch { }
        }

        /// <summary>
        /// 析构函数
        /// </summary>
        ~ImportFileTask()
        {
            Dispose();
        }
    }
}
